/***********************************************************************
 * Module:  ControlTaskService.java
 * Author:  Administrator
 * Purpose: Defines the Class ControlTaskService
 ***********************************************************************/

package ishitong.rvims.control.service;

import ishitong.rvims.control.persistence.ControlTaskMapper;
import ishitong.core.common.SQLSelectWhere;
import ishitong.rvims.domain.control.ControlPoint;
import ishitong.rvims.domain.control.ControlTask;
import ishitong.rvims.domain.control.ControlVehicle;
import ishitong.rvims.domain.inform.InformRule;
import java.util.HashMap;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/** @pdOid c258e1eb-36c5-43a3-a852-9a7a68225640 */
@Service
public class ControlTaskService {
   /** @pdOid db0f03e5-1e5a-421f-bdfd-14796bf27d59
       @pdRoleInfo migr=yes name=ControlTaskMapper assc=association5 */
   @Autowired
   private ControlTaskMapper controlTaskMapper;
   /** @pdOid bdc54ef8-0454-4aa5-aa73-9b07b8408738
       @pdRoleInfo migr=yes name=ControlTaskToVehicleService assc=association7 */
   @Autowired
   private ControlTaskToVehicleService controlTaskToVehicleService;
   /** @pdOid 123241ba-dbdc-4fa0-b61a-3f04392a1a85
       @pdRoleInfo migr=yes name=ControlTaskToPointService assc=association12 */
   @Autowired
   private ControlTaskToPointService controlTaskToPointService;
   /** @pdOid 1efabb70-465f-4214-8800-e409b80d9b71
       @pdRoleInfo migr=yes name=ControlTaskToInformRuleService assc=association11 */
   @Autowired
   private ControlTaskToInformRuleService controlTaskToInformRuleService;
   
   /** @param sQLSelectWhere
    * @exception Exception
    * @pdOid f8e51e81-06ba-4a94-925e-3d08e65e74a4 */
   public List<ControlTask> findControlTaskList(SQLSelectWhere sQLSelectWhere) throws Exception {
    return controlTaskMapper.selectControlTaskList(sQLSelectWhere);
   }
   
   /** @param sQLSelectWhere
    * @exception Exception
    * @pdOid 3e527173-c385-484a-84fc-83e802f86fce */
   public int getControlTaskTotal(SQLSelectWhere sQLSelectWhere) throws Exception {
      return controlTaskMapper.selectControlTaskTotal(sQLSelectWhere);
   }
   
   /** @param controlTaskId
    * @exception Exception
    * @pdOid 89747cd7-1ebb-4d9b-a299-db0af2e3f056 */
   public ControlTask getControlTaskById(String controlTaskId) throws Exception {
      // TODO: implement
    ControlTask controlTask =  controlTaskMapper.selectControlTaskById(controlTaskId);
    controlTask.setListControlPoint(controlTaskToPointService.findControlPointByControlTaskId(controlTaskId));
    controlTask.setListControlVehicle(controlTaskToVehicleService.findControlVehicleByControlTaskId(controlTaskId));
    controlTask.setListInformRule(controlTaskToInformRuleService.findInformRuleByControlTaskId(controlTaskId));
    return controlTask;
   }
   
   /** @param controlTask
    * @exception Exception
    * @pdOid a5b5fbaa-d807-4ffd-98cb-fd76a6a61ff0 */
   public void saveControlTask(ControlTask controlTask) throws Exception {
      // TODO: implement
    if (StringUtils.isBlank(controlTask.getId())) {
     	  controlTaskMapper.insertControlTask(controlTask);
     	 
       } else {
     	  controlTaskMapper.updateControlTask(controlTask);
       }
    controlTaskToPointService.deleteControlPointByControlTaskId(controlTask.getId());
    controlTaskToVehicleService.deleteControlVehicleByControlTaskId(controlTask.getId());
    controlTaskToInformRuleService.deleteInformRuleByControlTaskId(controlTask.getId());
    String[] controlTaskIds = new String[1];
    List<ControlPoint> listControlPoint = controlTask.getListControlPoint();
    String[] controlPointIds = new String[listControlPoint.size()];
    List<ControlVehicle> listControlVehicle = controlTask.getListControlVehicle();
    String[] controlVehicleIds = new String[listControlVehicle.size()];
    List<InformRule> listInformRule = controlTask.getListInformRule();
    String[] informRuleIds = new String[listInformRule.size()];
    controlTaskIds[0] = controlTask.getId();
    for(int index = 0 ; index < controlPointIds.length ; index ++)
    {
     controlPointIds[index] = listControlPoint.get(index).getId();
    }
    for(int index = 0 ; index < controlVehicleIds.length ; index ++)
    {
     controlVehicleIds[index] = listControlVehicle.get(index).getId();
    }
    for(int index = 0 ; index < informRuleIds.length ; index ++)
    {
     informRuleIds[index] = listInformRule.get(index).getId();
    }
    controlTaskToPointService.updateControlTaskToPoint(controlTaskIds,controlPointIds);
    controlTaskToVehicleService.updateControlTaskToVehicle(controlVehicleIds, controlTaskIds);
    controlTaskToInformRuleService.updateControlTaskToInformRule(controlTaskIds, informRuleIds);
    /*List<ControlPoint> listControlPoint = controlTask.getListControlPoint();
    HashMap<String,Integer> cphm = new HashMap<String,Integer>();
    for(ControlPoint cp : listControlPoint)
    {
    cphm.put("ID", Integer.parseInt(controlTask.getId()));
    cphm.put("T_C_ID", Integer.parseInt(cp.getId()));
    controlTaskToPointService.insertControlTaskToPoint(cphm);
    }*/
   
   
   }
   
   /** @param controlTaskIds
    * @exception Exception
    * @pdOid 9330ce91-251d-40e9-9d77-6cfa50f5e87a */
   public void deleteControlTaskByIds(String controlTaskIds) throws Exception {
      // TODO: implement
    controlTaskMapper.deleteControlTaskByIds(controlTaskIds);
   }
   
   /** @param controlTaskIds 
    * @param isUsing
    * @exception Exception
    * @pdOid d11cba79-a04a-46c8-a0c8-c11ade8b290e */
   public void setControlTaskUseState(String controlTaskIds, String isUsing) throws Exception {
      // TODO: implement
    HashMap<String,String>hm = new HashMap<String, String>();
    hm.put("isUsing", isUsing);
    hm.put("Ids", controlTaskIds);
    controlTaskMapper.updateControlTaskUseState(hm);
   }

}